#include "CoreImageData.h"
#include "GuidedNativeImageIO.h"
#include "ImageCoordinateGeometry.h"

#include <QPoint>
#include <QString>
#include <QMessageBox>

CoreImageData::CoreImageData()
{
    m_GreyWrapper = new GreyImageWrapper;
    m_SegWrapper = new SegImageWrapper;
    m_VisWrapper = new VisImageWrapper;

    m_ImageWrappers.push_back(m_GreyWrapper);
    m_ImageWrappers.push_back(m_SegWrapper);
    m_ImageWrappers.push_back(m_VisWrapper);

    connect(m_VisWrapper, SIGNAL(SeedPointAccepted(double,double,double)),
            this, SLOT(SeedPointPicked(double,double,double)));
}

CoreImageData::~CoreImageData()
{
    delete m_GreyWrapper;
    delete m_SegWrapper;
    delete m_VisWrapper;
}

void
CoreImageData::LoadMainImage(GuidedNativeImageIO &guidedIO)
{
    for (WrapperIterator it = m_ImageWrappers.begin(); it != m_ImageWrappers.end(); it++)
    {
        (*it)->SetImageProperties(guidedIO.GetImageProperties());
        (*it)->SetImageGeometry(guidedIO.GetImageGeometry());
    }
        m_GreyWrapper->SetImage(guidedIO.GetImageBase());
        m_SegWrapper->SetImage(m_GreyWrapper->GetImage());
        m_VisWrapper->SetImage(m_GreyWrapper->GetImage());
}

void
CoreImageData::WorkingMaterialChosen(int state, int label)
{
    if (state == 2)
    {
        m_SegWrapper->SetWorkingMaterial(label);
        if (m_SegWrapper->IsSegmentationDone(label))
            m_VisWrapper->ShowVolume(label);
    }

    else if (state == 0)
    {
        m_SegWrapper->UnsetWorkingMaterial();
        m_VisWrapper->HideVolume(label);
    }
}

void CoreImageData::SetWorkingSegmentation(SegImageWrapper::SegmentationType segType)
{
    m_SegWrapper->SetSegmentationType(segType);
}

void CoreImageData::SeedPointPicked(double x, double y, double z)
{
    if((m_SegWrapper->GetWorkingMaterial() != NULL)&&(m_SegWrapper->GetWorkingSegmentation() !=
                                                      SegImageWrapper::NOT_CHOSEN))
    {
        m_SegWrapper->SeedPointPicked(x, y, z);
        m_VisWrapper->SetVolume(m_SegWrapper->GetWorkingMaterial());
        m_VisWrapper->ShowVolume(m_SegWrapper->GetWorkingMaterial()->GetLabel());
    }
}
